package great.easychat.help.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import com.tencent.rtmp.TXLiveConstants
import com.tencent.rtmp.TXLivePlayConfig
import com.tencent.rtmp.TXLivePlayer
import great.easychat.help.BaseActivity
import great.easychat.help.R
import great.easychat.help.viewModel.SplashViewModel
import kotlinx.android.synthetic.main.activity_video_player.*
import java.net.URLDecoder


class VideoPlayerActivity : BaseActivity<SplashViewModel>()  {

    override fun observeLiveData() {}

    override fun requestLayoutId(): Int {
        return R.layout.activity_video_player
    }

    override fun isToolBarEnable(): Boolean {
        return false
    }
    lateinit var mTXLivePlayer:TXLivePlayer
    override fun setViewData(savedInstanceState: Bundle?): Unit {
        var url = intent.getStringExtra("url")
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        mTXLivePlayer = TXLivePlayer(this)
        //配置播放器属性
        //配置播放器属性
        mTXLivePlayer.setPlayerView(videoVideo)
        mTXLivePlayer.setRenderRotation(TXLiveConstants.RENDER_ROTATION_PORTRAIT)
        mTXLivePlayer.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN)

        val mTXPlayConfig = TXLivePlayConfig() //播放器的配置

        mTXPlayConfig.setAutoAdjustCacheTime(true)
        mTXPlayConfig.setMinAutoAdjustCacheTime(1f)
        mTXPlayConfig.setMaxAutoAdjustCacheTime(5f)
        mTXLivePlayer.setConfig(mTXPlayConfig) //添加配置

        if (!TextUtils.isEmpty(url)) {
            // 改成自己服务器的Url
            mTXLivePlayer.startPlay(URLDecoder.decode(url,"UTF-8"), TXLivePlayer.PLAY_TYPE_VOD_MP4)
        }
        videoVideo.setOnClickListener {
            if (mTXLivePlayer.isPlaying) {
                ivPlay.visibility = View.VISIBLE
                mTXLivePlayer.pause()
            } else {
                ivPlay.visibility = View.GONE
                mTXLivePlayer.resume()
            }
        }

        ivPlay.setOnClickListener {
            ivPlay.visibility = View.GONE
            mTXLivePlayer.resume()
        }
        ivBack.setOnClickListener {
            finish()
        }
    }


    override fun onDestroy() {
        mTXLivePlayer.stopPlay(true)
        videoVideo.onDestroy()
        super.onDestroy()
    }

}


